/*
 * Copyright (c) Created by T C L on 2019.7.9.
 */

package cn.mumzone.pos.presenter;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.event.SettleSuccessEvent;
import cn.mumzone.pos.model.SalesTimesCard;
import cn.mumzone.pos.model.source.MemberRepository;
import cn.mumzone.pos.presenter.contract.SaleCardContract;
import rx.Subscription;
import rx.functions.Action1;

public class SaleCardPresenter extends AbsPresenter<SaleCardContract.View, MemberRepository> implements SaleCardContract.Presenter {

    @Override
    public void getSaleTimeCardList(String pageNum, String pageSize, final boolean isLoadMore) {
        ApiCallBack<PageListResponse<SalesTimesCard>> salesTimesCardCallback =
                new ApiCallBack<PageListResponse<SalesTimesCard>>() {
                    @Override
                    public void onSuccess(PageListResponse<SalesTimesCard> timesCard) {
                        if (!isLoadMore) {
                            mView.setSalesTimesCardData(timesCard);
                        } else {
                            mView.appendSalesTimesCardData(timesCard);
                        }
                        mView.dismissLoading();
                    }

                    @Override
                    public void onError(BaseResponse response) {
                        mView.showError(response.getMsg());
                        mView.dismissLoading();
                    }
                };
        mRepo.getSaleTimeCardList(pageNum, pageSize, salesTimesCardCallback);
        addSubscrebe(salesTimesCardCallback);
    }

    public void onEvent() {  // 付款成功Event
        Subscription subscription = RxBus.getDefault()
                .toObservable(SettleSuccessEvent.class)
                .subscribe(new Action1<SettleSuccessEvent>() {
                    @Override
                    public void call(SettleSuccessEvent settleSuccessEvent) {
                        mView.onPaySuccess();
                    }
                });
        addSubscrebe(subscription);
    }
}
